package com.example.bingobox.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Task;
import java.util.List;
import java.util.Locale;

public class TimeLabelAdapter extends RecyclerView.Adapter<TimeLabelAdapter.ViewHolder> {
    private final List<Task.TimeLabel> timeLabels;
    private OnTimeLabelActionListener listener;

    public interface OnTimeLabelActionListener {
        void onLabelClick(Task.TimeLabel timeLabel, int position);
        void onDelete(Task.TimeLabel timeLabel, int position);
        void onEdit(Task.TimeLabel timeLabel, int position);
    }

    public TimeLabelAdapter(List<Task.TimeLabel> timeLabels, OnTimeLabelActionListener listener) {
        this.timeLabels = timeLabels;
        this.listener = listener;
    }

    public void setActionListener(OnTimeLabelActionListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_time_label, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Task.TimeLabel timeLabel = timeLabels.get(position);
        
        // 格式化时间显示
        String timeText = String.format(Locale.getDefault(), "%s %02d:%02d-%02d:%02d",
            timeLabel.getDayType(),
            timeLabel.getStartHour(),
            timeLabel.getStartMinute(),
            timeLabel.getEndHour(),
            timeLabel.getEndMinute());
            
        holder.timeLabelText.setText(timeText);
        
        holder.editButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(timeLabel, holder.getAdapterPosition());
            }
        });
        
        holder.deleteButton.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(timeLabel, holder.getAdapterPosition());
            }
        });
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onLabelClick(timeLabel, holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return timeLabels.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView timeLabelText;
        ImageButton editButton;
        ImageButton deleteButton;

        ViewHolder(View itemView) {
            super(itemView);
            timeLabelText = itemView.findViewById(R.id.text_time_label);
            editButton = itemView.findViewById(R.id.btn_edit);
            deleteButton = itemView.findViewById(R.id.btn_delete);
        }
    }
} 